const { Model, DataTypes } = require('sequelize');

// 导出初始化函数
module.exports = (sequelize) => {
    class Category extends Model {}

    // 初始化模型
    Category.init({
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING(100),
            allowNull: false,
            unique: true,
            validate: {
                notEmpty: {
                    msg: '分类名称不能为空'
                },
                len: {
                    args: [2, 100],
                    msg: '分类名称长度必须在2-100个字符之间'
                }
            }
        },
        description: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        image: {
            type: DataTypes.STRING(255),
            allowNull: true
        },
        parentId: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'categories',
                key: 'id'
            }
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: true
        }
    }, {
        sequelize,
        modelName: 'Category',
        tableName: 'categories',
        timestamps: true
    });

    return Category;
};